package com.example.music_liwei.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.music_liwei.R;
import com.example.music_liwei.activity.MusicMainActivity;
import com.example.music_liwei.bean.ResponseData;
import com.example.music_liwei.messageEvent.MessageEventB;
import com.example.music_liwei.messageEvent.MessageEventCurrentIndexBoolean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class BlankFragmentShowAll extends Fragment {

    private List<ResponseData.MusicInfo> musicInfoList;
    private int currentIndex;
    private boolean isPlay;

    private ImageView imageViewCover;
    private TextView textViewMusicName;
    private TextView textViewAuthor;
    private ImageView imPlay, ivCaidan;

    private float initialY;
    private boolean isDraggingUp = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this); // 注册 EventBus
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blank_show_all, container, false);

        imageViewCover = view.findViewById(R.id.imageView_cover);
        textViewMusicName = view.findViewById(R.id.textView_music_name);
        textViewAuthor = view.findViewById(R.id.textView_author);
        imPlay = view.findViewById(R.id.im_play);
        ivCaidan = view.findViewById(R.id.iv_caidan);

        ivCaidan.setOnClickListener(v -> {
            if (getActivity() instanceof MusicMainActivity) {
                ((MusicMainActivity) getActivity()).onMenuIconClick();
            }
        });

        imPlay.setOnClickListener(v -> {
            EventBus.getDefault().postSticky(new MessageEventB());
        });

        view.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    initialY = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    float deltaY = event.getY() - initialY;
                    if (deltaY < 0) {
                        isDraggingUp = true;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    if (isDraggingUp) {
                        if (getActivity() instanceof MusicMainActivity) {
                            ((MusicMainActivity) getActivity()).onSwipeUp();
                        }
                        isDraggingUp = false;
                    }
                    break;
            }
            return true;
        });

        return view;
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEventCurrentIndexBoolean event) {
        musicInfoList = event.getMusicInfoList();
        currentIndex = event.getCurrentIndex();
        isPlay = event.getIsPlay();

        // 更新视图
        if (musicInfoList != null && !musicInfoList.isEmpty()) {
            ResponseData.MusicInfo currentMusic = musicInfoList.get(currentIndex);
            textViewMusicName.setText(currentMusic.getMusicName());
            textViewAuthor.setText(currentMusic.getAuthor());
            imPlay.setImageResource(isPlay ? R.drawable.play : R.drawable.player);

            // 使用RequestOptions设置圆角属性
            RequestOptions options = new RequestOptions()
                    .transform(new CircleCrop());  // 使用CircleCrop进行圆形转换
            Glide.with(this)
                    .load(musicInfoList.get(currentIndex).getCoverUrl())
                    .apply(options)
                    .into(imageViewCover);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this); // 注销 EventBus
    }

    // 定义接口
    public interface OnMenuIconClickListener {
        void onMenuIconClick();
    }
}
